<?php
/**
 * 
 * @param number $classid
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_init_rollup($scheduleid)
{
	global $wpdb;
	$current_schedule_data = seitcm_get_single_schedule($scheduleid);
	$classid = $current_schedule_data->classid;
	$student_list = seitcm_get_class_student_list($classid);
	$wpdb->query('start transaction');
	foreach ($student_list as $student)
	{
		$sql = $wpdb->prepare('INSERT INTO ' . SEITCM_TABLE_ROLLUP .' (studentid,scheduleid) VALUES (%d,%d)',$student->ID,$scheduleid);
		$wpdb->query($sql);
	}
	return $wpdb->query('commit');
}
function seitcm_clear_interrupt_rollup($scheduleid){
	global $wpdb;
	$sql = $wpdb->prepare('DELETE FROM ' . SEITCM_TABLE_ROLLUP . ' WHERE scheduleid = %d',$scheduleid);
	return $wpdb->query($sql);
}
/**
 * 
 * @param number $classid
 * @return Ambigous <string, NULL>
 */
function seitcm_check_exist_record_rollup($scheduleid){
	global $wpdb;
	$sql = $wpdb->prepare('SELECT COUNT(id) FROM ' . SEITCM_TABLE_ROLLUP . ' WHERE scheduleid = %d',$scheduleid);
	$numrolluprecord = $wpdb->get_var($sql);
	if($numrolluprecord==0)
	{
		return false;
	}
	return true;
}
/**
 * 
 * @param number $classid
 */
function seitcm_check_init_rollup($scheduleid){
	if(!seitcm_check_exist_record_rollup($scheduleid))
	{
		seitcm_clear_interrupt_rollup($scheduleid);
		seitcm_init_rollup($scheduleid);
	}
}
/**
 * 
 * @param unknown $scheduleid
 * @param unknown $rolluparray
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_update_rollup($scheduleid,$rolluparray){
	global $wpdb;
	$wpdb->query('start transaction');
	foreach ($rolluparray as $rollup)
	{
		$sql = $wpdb->prepare('UPDATE ' . SEITCM_TABLE_ROLLUP .' SET note = %s, checkin = %d WHERE studentid = %d AND scheduleid = %d',$rollup['comment'],$rollup['rollup'],$rollup['stuid'],$scheduleid);
		$wpdb->query($sql);
	}
	return $wpdb->query('commit');
}
/**
 * 
 * @param unknown $scheduleid
 * @return Ambigous <mixed, NULL, multitype:, multitype:multitype: , multitype:Ambigous <multitype:, NULL> >
 */
function seitcm_get_rollup_table($scheduleid){
	global $wpdb;
	$sql = $wpdb->prepare('SELECT * FROM ' . SEITCM_TABLE_ROLLUP . ' WHERE scheduleid = %d',$scheduleid);
	return $wpdb->get_results($sql);
}
function seitcm_get_rollup_status_list($studentid,$classid){
	global $wpdb;
	$sql = $wpdb->prepare('SELECT A.`scheduleid`,A.`checkin`,B.`time`,B.`title` FROM '.SEITCM_TABLE_ROLLUP.' A INNER JOIN '.SEITCM_TABLE_SCHEDULE
			.' B ON A.scheduleid = B.id WHERE A.`studentid` = %d AND B.`classid` = %d AND B.`status` = 1',$studentid,$classid);
	return $wpdb->get_results($sql);
}
function seitcm_get_rollup_status($rollup_status_list,$scheduleid){
	foreach ($rollup_status_list as $rollup){
		if($rollup->scheduleid==$scheduleid){
			return $rollup->checkin;
		}
	}
	return false;
}
?>